package com.ftwj.demo.modular.controller;


import javax.validation.constraints.Positive;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ftwj.demo.global.aop.Logger;
import com.ftwj.demo.base.util.PageInfoBean;
import com.ftwj.demo.config.swagger.DataType;
import com.ftwj.demo.config.swagger.HttpMethod;
import com.ftwj.demo.config.swagger.ParamType;
import com.ftwj.demo.modular.entity.ScheduleJobBean;
import com.ftwj.demo.modular.service.ScheduleJobService;
import com.ftwj.demo.utils.result.MessageEnum;
import com.ftwj.demo.utils.result.ResultBean;
import com.ftwj.demo.utils.validate.ValidateGroupKey;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
*
* <p>Title: ScheduleJobController</p>  
* <p>Description: 描述: 调度任务前端控制器</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author WangSli
* @date 2020年04月16日 14:02:35
* @version V1.0
*/
@RestController 
@RequestMapping("/scheduleJob")
@Api(tags="调度任务接口", value="调度任务接口")
@Validated
public class ScheduleJobController {

	@Autowired
	private ScheduleJobService service;

	/**
	 * @author  WangSli
	 * @date 2020年04月16日 14:02:35
	 * @version 1.0
	 */
	@GetMapping("page")
	@ApiOperation(value="调度任务分页列表数据条件查询（TODO）", notes="备注",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
    	@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
    	,@ApiImplicitParam(name="size", value="每页显示条数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
    })
	public ResultBean<?> page(@RequestParam(defaultValue="1")@Positive Integer current, @RequestParam(defaultValue="10")@Positive Integer size
			, @PathVariable(required = false) String jobName, @PathVariable(required = false) String groupName) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		ScheduleJobBean bean=new ScheduleJobBean();
		bean.setName(jobName).setGroup(groupName);
		pageInfo = service.page(bean,pageInfo);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * @author  WangSli
	 * @date 2020年04月16日 14:02:35
	 * @version 1.0
	 */
	@GetMapping("/info/{id}")
	@ApiOperation(value="获取调度任务详细信息（DONE）", notes="备注",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="编号", dataType=DataType.INTEGER, paramType=ParamType.PATH, required=false)
	})
	public ResultBean<ScheduleJobBean> info(@PathVariable@Positive Integer id){
		ScheduleJobBean data = service.findById(id);
		return ResultBean.success(data);
	}
	
	/**
	 * @author WangSli
	 * @date 2020年04月16日 14:02:35
	 * @version 1.0
	 */
	@PostMapping("/insert")
	@ApiOperation(value="新增调度任务，主要参数全部必填", notes="数据",code=0,httpMethod=HttpMethod.POST)
	@Logger //日志记录自定义注解
	public ResultBean<?> insert(@RequestBody@Validated ScheduleJobBean entity){
		if(service.add(entity) > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: edit</p>  
	 * <p>Description: 数据修改</p>  
	 * @param entity 修改的对象（主键必须不为空）
	 * @author HeYang
	 * @date 2020年04月16日 14:02:35  
	 * @version 1.0
	 */
	@PutMapping("/edit")
	@ApiOperation(value="调度任务修改，仅能通过id，任务名，分组名修改cron表达式，不能修改其他", notes="",code=0,httpMethod=HttpMethod.PUT)
	@Logger //日志记录自定义注解
	public ResultBean<?> edit(@RequestBody@Validated(ValidateGroupKey.class) ScheduleJobBean entity){
		if(service.save(entity) > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: delete</p>  
	 * <p>Description: 数据删除(逻辑)</p>  
	 * @param id 主键
	 * @author HeYang
	 * @date 2020年04月16日 14:02:35  
	 * @version 1.0
	 */
	@DeleteMapping("/delete/{id}")
	@ApiOperation(value="调度任务删除", notes="数据",code=0,httpMethod=HttpMethod.DELETE)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="编号", dataType=DataType.INTEGER, paramType=ParamType.PATH)
	})
	@Logger //日志记录自定义注解
	public ResultBean<?> delete(@Positive(message="ID必须是大于0的整数")@PathVariable Integer id) {
		if(service.remo(id)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: deles</p>  
	 * <p>Description: 批量删除</p>  
	 * @param ids 要删除数据的ID数组
	 * @author HeYang
	 * @date 2020年04月16日 14:02:35  
	 * @version 1.0
	 */
	@DeleteMapping("/deles")
	@ApiOperation(value="调度任务批量删除", notes="数据",code=0,httpMethod=HttpMethod.DELETE)
	@Logger //日志记录自定义注解
	public ResultBean<?> deles(@RequestBody Integer[] ids){
		service.remo(ids);
		return ResultBean.success();
	}
	
	/**
	 * <p>Title: pause</p>  
	 * <p>Description: 暂停单个调度任务</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月16日  
	 * @version 1.0
	 */
	@GetMapping("/pause/{id}")
	@ApiOperation(value="暂停调度任务", notes="备注",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="编号", dataType=DataType.INTEGER, paramType=ParamType.PATH, required=false)
	})
	public ResultBean<?> pause(@PathVariable@Positive Integer id){
		if(service.pause(id) > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: pause</p>  
	 * <p>Description: 暂停所有调度任务</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月16日  
	 * @version 1.0
	 */
	@GetMapping("/pause")
	@ApiOperation(value="暂停所有调度任务", notes="备注",code=0,httpMethod=HttpMethod.GET)
	public ResultBean<?> pause(){
		if(service.pause() > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: restart</p>  
	 * <p>Description: 恢复单个调度任务</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月16日  
	 * @version 1.0
	 */
	@GetMapping("/restart/{id}")
	@ApiOperation(value="恢复单个调度任务", notes="备注",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="编号", dataType=DataType.INTEGER, paramType=ParamType.PATH, required=false)
	})
	public ResultBean<?> restart(@PathVariable@Positive Integer id){
		if(service.restart(id) > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: restart</p>  
	 * <p>Description: 恢复所有调度任务</p>  
	 * @author HeYang
	 * @date 2020年4月16日  
	 * @version 1.0
	 */
	@GetMapping("/restart")
	@ApiOperation(value="恢复所有调度任务", notes="备注",code=0,httpMethod=HttpMethod.GET)
	public ResultBean<?> restart(){
		if(service.restart() > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	
}
